package dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import bean.User;


/**
 * A data access object (DAO) providing persistence and search support for User
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see bean.User
 * @author MyEclipse Persistence Tools
 */

public class UserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserDAO.class);
	// property constants
	public static final String USERFNAME = "userfname";
	public static final String USERLNAME = "userlname";
	public static final String GENDER = "gender";
	public static final String EMAIL = "email";
	public static final String PASSWORD = "password";
	public static final String USERLOCATION = "userlocation";

	public void save(User transientInstance) {
		Transaction ts=null;
		log.debug("saving User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().save(transientInstance);
			log.debug("save successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("save failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}	
	public void update(User transientInstance) {
		Transaction ts=null;
		log.debug("saving User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().update(transientInstance);
			log.debug("save successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("save failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}
	public void delete(User persistentInstance) {
		Transaction ts=null;
		log.debug("deleting User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().delete(persistentInstance);
			log.debug("delete successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("delete failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}

	public User findById(java.lang.Integer id) {
		Transaction ts=null;
		User instance=null;
		log.debug("getting User instance with id: " + id);
		try {
			ts=getSession().beginTransaction();
			instance = (User) getSession().get("bean.User", id);
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("get failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return instance;
	}

	public List findByExample(User instance) {
		Transaction ts=null;
		List results=null;
		log.debug("finding User instance by example");
		try {
			ts=getSession().beginTransaction();
			results = getSession().createCriteria("bean.User").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find by example failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return results;
	}

	public List findByProperty(String propertyName, Object value) {
		Transaction ts=null;
		List result=null;
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			ts=getSession().beginTransaction();
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			result=queryObject.list();
			ts.commit();			
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find by property name failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return result;
	}

	public List findByUserfname(Object userfname) {
		return findByProperty(USERFNAME, userfname);
	}

	public List findByUserlname(Object userlname) {
		return findByProperty(USERLNAME, userlname);
	}

	public List findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByUserlocation(Object userlocation) {
		return findByProperty(USERLOCATION, userlocation);
	}

	public List findAll() {
		Transaction ts=null;
		List results=null;
		log.debug("finding all User instances");
		try {
			ts=getSession().beginTransaction();
			String queryString = "from User";
			Query queryObject = getSession().createQuery(queryString);
			results=queryObject.list();
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return results;
	}

	public User merge(User detachedInstance) {
		Transaction ts=null;
		User result=null;
		log.debug("merging User instance");
		try {
			ts=getSession().beginTransaction();
			result = (User) getSession().merge(detachedInstance);
			log.debug("merge successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("merge failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return result;
	}

	public void attachDirty(User instance) {
		Transaction ts=null;
		log.debug("attaching dirty User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("attach failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}

	public void attachClean(User instance) {
		Transaction ts=null;
		log.debug("attaching clean User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("attach failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}
}